
package com.ukao.steward.pesenter.valetRunners;

import com.ukao.steward.base.BasePresenter;
import com.ukao.steward.bean.RepetitionBean;
import com.ukao.steward.bean.ShoppingBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.bean.businessWholeBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.view.valetRunners.shoppingMallView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import rx.Observable;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class shoppingMallPesenter extends BasePresenter<shoppingMallView> {


    public shoppingMallPesenter(shoppingMallView view) {
        attachView(view);
    }


    /**
     * 获取分类
     */
    public void businessWholeTab() {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParams.getToken());
        Observable observable = null;
        observable = apiStores.businessWhole(Constant.createParameter(mHashMap));
        addSubscription(observable,
                new ApiCallback<businessWholeBean>() {
                    @Override
                    public void onSuccess(businessWholeBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.businessWholeSucceed(model.getData());


                        } else {

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }





    /**
     * 生成数据
     * @param mData  衣物数据
     * @param isValuationEnter  是否精准计价 进入
     *
     */
    public    void  createOrder(List<ShoppingBean> mData  ,boolean  isValuationEnter   ){

        ArrayList<StringBean>   mStringBeanData=new ArrayList<>();
        List<RepetitionBean> mPosData = new ArrayList<>();
        int   clodiscount=0;
        for (ShoppingBean item : mData) {
            int count = item.getCount();
            if (count != 0) {
                //'打折类型 1.会员折扣 2.自定义 3.无任何打折',
                clodiscount=item.getSaleDiscount();
                StringBean  mStringBean=new StringBean(item.getMercBusinessId(),item.getBusinessId(),item.getBusinessName(),item.getTypeId(),
                        item.getId()+"",item.getImgPath(),item.getName(),item.getCount(),
                        item.getPrice(),clodiscount,item.getSelectServiceName(),item.getSelectServiceId());
                mStringBeanData.add(mStringBean);
                if (!isValuationEnter){
                    mPosData.add(new RepetitionBean(item.getBusinessName(), item.getBusinessId()));
                }
            }
        }

        if (!isValuationEnter){/*添加tab 的josn 数据*/
            Set<RepetitionBean> setData = new HashSet<RepetitionBean>();
            setData.addAll(mPosData);
            String  service="";
            JSONArray mBusinessArray = new JSONArray();
            StringBuffer mServeBuffer = new StringBuffer();
            for (RepetitionBean shoppingBean : setData) {
                JSONObject mBusinessject = new JSONObject();
                try {
                    mBusinessject.put("id",shoppingBean.getBusinessId());
                    mBusinessject.put("name",shoppingBean.getBusinessName());
                    mServeBuffer.append(shoppingBean.getBusinessName() + "/");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                mBusinessArray.put(mBusinessject);
            }
            if (mServeBuffer.length() != 0)
                service = mServeBuffer.deleteCharAt(mServeBuffer.length() - 1).toString();
            mvpView.createOrderSucceed(mBusinessArray.toString(), mStringBeanData,service);
        }else if (isValuationEnter){
            mvpView.valuationSucceed( mStringBeanData);
        }



    }
















}
